﻿<!DOCTYPE html>
<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!-->
<html class="no-js">
<!--<![endif]-->

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>企业微信管理后台</title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="../../css/reset.css">
    <link rel="stylesheet" href="../../css/fonts.css">
    <link rel="stylesheet" href="../../js/bootstrap/css/bootstrap.min.css">
    <!-- bootstrap表格 -->
    <link rel="stylesheet" href="../../js/bootstrap/js/bootstrap-table-master/bootstrap-table.min.css">
    <link rel="stylesheet" href="../../js/layui/css/layui.css">
    <link rel="stylesheet" href="../../css/main.css">
</head>

<!--企业全部客户-->

<body>
    <div id="customer-list">
        <div class="header">
            <div class="type_select_wrapper">DCMS全部客户</div>
        </div>
        <div class="container">
            <div class="screen_wrapper">
                <div class="screen_item logo_wrapper">
                    <span class="text">服务人员：</span>
                    <div class="select_style logo_select_div">不限</div>
                </div>
                <div class="screen_item time_select_wrapper">
                    <span class="text">时间：</span>
                    <div class="time_wrapper">
                        <input class="layui-input" type="text" placeholder="开始时间" id="startDate">
                        <span class="text units">至</span>
                        <input class="layui-input" type="text" placeholder="结束时间" id="endDate">
                    </div>
                </div>
            </div>
            <div class="send_item screen_excel_import" id="screen_excel_import">
                <div class="excel_wrapper">
                    <div class="select_excel_box">
                        <div class="select_text">点击导入</div>
                        <input class="excel_input" id="excel_input" name="excel_input" type="file"
                            accept="application/vnd.ms-excel,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet">
                    </div>
                    <span class="input_file_name" style="display: none;"></span>
                    <a class="download_template" href="javascript:;">下载模板</a>
                </div>
            </div>
            <!-- 客户列表 -->
            <div class="logo_cus_wrapper">
                <div class="table_header">
                    <span class="cus_num"></span>
                    <a href="javascript:;" class="exporting_table" id="exporting_table">导出客户列表</a>
                </div>
                <div class="dcms_table_box">
                    <table class="layui-table dcms_customer_table" style="table-layout:fixed">
                        <thead>
                            <tr>
                                <th class="cus_name">客户真实姓名</th>
                                <th class="vin">车架号</th>
                                <th class="lsn">车牌号</th>
                                <th class="car_type">车型</th>
                                <th class="sa">服务人员</th>
                                <th class="import_time">导入时间</th>
                                <th class="in_wechat">是否加入企业微信</th>
                            </tr>
                        </thead>
                        <tbody id="tby">
                        </tbody>
                        <!-- <tbody>
                            <tr>
                                <td>李白</td>
                                <td>VIN12345678901234</td>
                                <td>湘A12345</td>
                                <td>奕歌</td>
                                <td>李残影</td>
                                <td>2020-03-16 13:00:00</td>
                                <td>否</td>
                            </tr>
                        </tbody> -->
                    </table>
                </div>
                <div class="paging_style" id="paging"></div>
            </div>
        </div>
    </div>

    <!-- 选择车型模态框 -->
    <!--<div class="modal fade normal-modal" id="select_modelname_modal" tabindex="-1" role="dialog"
         aria-labelledby="select_logo_modal_label">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <form class="layui-form">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                        <h4 class="modal-title" id="select_logo_modal_label">选择车型</h4>
                    </div>
                    <div class="modal-body">
                        <div class="select_logos_wrapper">
                            <div class="logo_group_item">
                                <div class="group_name">客户车型</div>
                                <div class="logo_item" id="modelname_item">

                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <div  class="clear_all_selected_logo" id="clear_all_selected_modelname" style="display: none;">清除选中车型</div>
                        <div class="btn_wrapper">
                            <button type="button" class="btn btn-primary" id="submit_select_modelname">确定</button>
                            <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>-->
    <!-- 选择车型模态框 -->
    <!-- 选择服务人员模态框 -->
    <div class="modal fade normal-modal" id="select_logo_modal" tabindex="-1" role="dialog"
        aria-labelledby="select_logo_modal_label">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <form class="layui-form">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                        <h4 class="modal-title" id="select_logo_modal_label">选择服务人员</h4>
                    </div>
                    <div class="modal-body">
                        <div class="select_logos_wrapper">
                            <div class="logo_group_item">
                                <div class="group_name">服务人员</div>
                                <div class="logo_item">

                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <div class="clear_all_selected_logo" style="display: none;">清除选中车型</div>
                        <div class="btn_wrapper">
                            <button type="button" class="btn btn-primary" id="submit_select_logos">确定</button>
                            <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <!-- 选择服务人员模态框 -->

    <div class="mask-bg">
        <div class="mask-loading">
            <div class="sk-spinner sk-spinner-cube-grid">
                <div class="iconfont icon-jiazai"></div>
            </div>
            <div class="tip">加载中</div>
        </div>
    </div>

    <script src="../../js/jquery-3.3.1.min.js"></script>
    <script src="../../js/bootstrap/js/bootstrap.min.js"></script>
    <script src="../../js/layui/layui.js"></script>
    <!-- 树形结构 -->
    <script src="../../js/proTree.js"></script>
    <script src="../../js/Base.js"></script>

    <script>
        //获取url中的参数
        function getUrlParam(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
            var r = window.location.search.substr(1).match(reg); //匹配目标参数
            if (r != null) return unescape(r[2]);
            return null; //返回参数值
        }

        var arr = [];
        var Page_id = 1, Page_size = 5, ModelName = [], GetUserName = [], StartTime = getPreDate(100000), EndTime = getDate();

        // 监听 logo标签选择
        //let TagId = []
        // 获取当前日期
        function getDate() {
            var mydate = new Date();
            var str = "" + mydate.getFullYear() + "-";
            let month = "";
            let day = "";
            if (mydate.getMonth() < 10) {
                month = "0" + (mydate.getMonth() + 1);
            } else {
                month = mydate.getMonth() + 1;
            }
            if (mydate.getDate() < 10) {
                day = "0" + mydate.getDate();
            } else {
                day = mydate.getDate();
            }
            str += month + "-";
            str += day;
            return str;
        }
        // 昨天的日期
        function getPreDate(dayNum) {
            var day1 = new Date();
            day1.setTime(day1.getTime() - (dayNum || 1) * 24 * 60 * 60 * 1000);
            let month = "";
            let day = "";
            if (day1.getMonth() < 10) {
                month = "0" + (day1.getMonth() + 1);
            } else {
                month = day1.getMonth() + 1;
            }
            if (day1.getDate() < 10) {
                day = "0" + day1.getDate();
            } else {
                day = day1.getDate();
            }
            var s1 = day1.getFullYear() + "-" + month + "-" + day;
            return s1;
        }
        //
        layui.use('form', function () {
            var form = layui.form;
            layui.use('layer', function () {
                var layer = layui.layer;
                var todayDate = getDate() // 当前日期
                $(".cus_num").html("共0个客户,上次导入时间: 无");
                // 默认实例化分页
                // 表格分页
                layui.use('laypage', function () {
                    var laypage = layui.laypage;
                    laypage.render({
                        elem: 'paging',
                        limit: 5,
                        theme: '#E51C23',
                        count: 0, //数据总数，从服务端得到
                    })
                });
                //请求客户数据
                function ajaxPost() {
                    $(".mask-bg").show()
                    var getusernamelist = [];
                    for (var i = 0; i < GetUserName.length; i++) {
                        getusernamelist.push(GetUserName[i].id)
                    }
                    let Page = {
                        "Page_id": Page_id,
                        "Page_size": Page_size
                    }
                    let postdata = {
                        "ModelName": ModelName,
                        "GetUserName": getusernamelist,
                        "StartTime": StartTime,
                        "EndTime": EndTime,
                        "Page": Page
                    }
                    //console.log("postdata", postdata);
                    $.ajax({
                        url: Domain + "/FindDCMSCustomer",
                        type: "post",
                        data: postdata,
                        beforeSend: function (request) {
                            request.setRequestHeader("Authorization", Authorization);
                        },
                        success: function (res) {
                            $(".mask-bg").hide()
                            if (res) {
                                if (res.Response.Code == 200) {
                                    var data = res.Response.Data;
                                    var datainfo = data.ResultList;
                                    var css = ""; var lastImportTime = '';
                                    for (var i = 0; i < datainfo.length; i++) {
                                        if (i == 0) {
                                            lastImportTime = datainfo[i].CreateTime;
                                        };
                                        var corpCustomer = "未加入";
                                        if (datainfo[i].IsCorpCustomer) {
                                            corpCustomer = "已加入";
                                        }
                                        css += '<tr data-id="' + datainfo[i].CustomerId + '" cc-id="' + datainfo[i].MemberName + '" >' +
                                            '<td>' + datainfo[i].CustomerName + '</td>' +
                                            '<td>' + datainfo[i].VIN + '</td>' +
                                            '<td>' + datainfo[i].LSN + '</td>' +
                                            '<td>' + datainfo[i].ModelName + '</td>' +
                                            '<td>' + datainfo[i].MemberName + '</td>' +
                                            '<td>' + datainfo[i].CreateTime + '</td>' +
                                            '<td>' + corpCustomer + '</td>' +
                                            '</tr>';
                                    }
                                    $("#tby").html(css);
                                    $(".cus_num").html("共" + data.TotalRecords + "个客户,上次导入时间:" + lastImportTime);
                                    form.render();
                                    // 表格分页
                                    layui.use('laypage', function () {
                                        var laypage = layui.laypage;
                                        laypage.render({
                                            elem: 'paging',
                                            limit: 5,
                                            theme: '#E51C23',
                                            count: data.TotalRecords, //数据总数，从服务端得到
                                            jump: function (obj, first) {
                                                Page_id = obj.curr;
                                                Page_size = obj.limit;
                                                //首次不执行
                                                if (!first) {
                                                    $.ajax({
                                                        url: Domain + "/FindDCMSCustomer",
                                                        type: "post",
                                                        data: postdata,
                                                        beforeSend: function (request) {
                                                            request.setRequestHeader("Authorization", Authorization);
                                                        },
                                                        success: function (res) {
                                                            if (res) {
                                                                if (res.Response.Code == 200) {
                                                                    var data = res.Response.Data;
                                                                    var datainfo = data.ResultList;
                                                                    var css = "";
                                                                    for (var i = 0; i < datainfo.length; i++) {
                                                                        var corpCustomer = "未加入";
                                                                        if (datainfo[i].IsCorpCustomer) {
                                                                            corpCustomer = "已加入";
                                                                        }
                                                                        css += '<tr data-id="' + datainfo[i].CustomerId + '" cc-id="' + datainfo[i].MemberName + '" >' +
                                                                            '<td>' + datainfo[i].CustomerName + '</td>' +
                                                                            '<td>' + datainfo[i].VIN + '</td>' +
                                                                            '<td>' + datainfo[i].LSN + '</td>' +
                                                                            '<td>' + datainfo[i].ModelName + '</td>' +
                                                                            '<td>' + datainfo[i].MemberName + '</td>' +
                                                                            '<td>' + datainfo[i].CreateTime + '</td>' +
                                                                            '<td>' + corpCustomer + '</td>' +
                                                                            '</tr>';
                                                                    }
                                                                    $("#tby").html(css);
                                                                    $(".cus_num").html("共" + data.TotalRecords + "个客户");
                                                                    form.render();
                                                                }
                                                                else {
                                                                    FindLoginState(res.Response.Msg, "FindDCMSCustomer");
                                                                }
                                                            }
                                                        },
                                                        error: function (err) {
                                                            console.log(err)
                                                        }
                                                    })
                                                }
                                            }
                                        })
                                    });
                                }
                                else {
                                    FindLoginState(res.Response.Msg, "FindDCMSCustomer");
                                }
                            }
                        },
                        error: function (err) {
                            $(".mask-bg").hide()
                            console.log(err)
                        }
                    })
                }
                ajaxPost();
                layui.use('laydate', function () {
                    var laydate = layui.laydate;
                    // 开始日期
                    let startDateValue = laydate.render({
                        elem: '#startDate', //指定元素
                        value: "",
                        max: todayDate,
                        done: function (value, date) {
                            StartTime = value;
                            ajaxPost();
                        }
                    });
                    // 结束日期
                    let endDateValue = laydate.render({
                        elem: '#endDate', //指定元素
                        value: todayDate,
                        max: todayDate,
                        done: function (value, date) {
                            EndTime = value;
                            startDateValue.config.max = {
                                year: date.year,
                                month: date.month - 1,
                                date: date.date
                            }
                            ajaxPost();
                        }
                    });
                })
                function ajaxpostMember() {
                    //获取所有服务人员
                    $.ajax({
                        url: Domain + "/GetDCMSCusMemberList",
                        type: "get",
                        beforeSend: function (request) {
                            request.setRequestHeader("Authorization", Authorization);
                        },
                        success: function (data) {
                            if (data) {
                                if (data.Response.Code == 200) {
                                    var datainfo = data.Response.Data;
                                    $(".logo_item").empty();
                                    $.each(datainfo, function (i) {
                                        $(".logo_item").append('<input class="item" type="checkbox" lay-filter="logo_select" ' +
                                            'lay - skin="primary"name = "logo" value = "' + datainfo[i] + '" title = "' + datainfo[i] + '" > ');
                                    });
                                    form.on('checkbox(logo_select)', function (data) {
                                        let selected = {
                                            id: data.value,
                                            name: data.elem.title
                                        }
                                        if (data.elem.checked == true) {
                                            GetUserName.push(selected)
                                        } else {
                                            for (let i = 0; i < GetUserName.length; i++) {
                                                if (GetUserName[i].id == data.value) {
                                                    GetUserName.splice(i, 1)
                                                }
                                            }
                                        }
                                        if (GetUserName.length > 0) {
                                            $(".clear_all_selected_logo").show()
                                        } else {
                                            $("#clear_all_selected_logo").hide()
                                        }
                                    })
                                    // logo标签选择确认
                                    $("#submit_select_logos").click(function () {
                                        // 执行ajax操作
                                        ajaxPost();
                                        // 控制标签的显示
                                        let html = "";
                                        for (let i = 0; i < GetUserName.length; i++) {
                                            html += `${GetUserName[i].name},`
                                        }
                                        html = html.substring(0, html.length - 1)
                                        if (html == '') {
                                            $(".logo_select_div").text('不限')
                                        } else {
                                            $(".logo_select_div").text(html)
                                            $(".logo_select_div").attr('title', html)
                                        }
                                        $("#select_logo_modal").modal('hide')
                                    })
                                    // 清除所选的标签
                                    $(".clear_all_selected_logo").click(function () {
                                        $("input[name=logo]").each(function () {
                                            if ($(this).prop('checked') == true) {
                                                $(this).prop('checked', false)
                                            }
                                            form.render('checkbox')
                                        })
                                        GetUserName = []
                                    })
                                    form.render();
                                }
                                else {
                                    FindLoginState(data.Response.Msg, "GetDCMSCusMemberList");
                                }
                            }
                        },
                        error: function (err) {
                            console.log(err)
                        }
                    })
                }
                ajaxpostMember();
                // 打开服务人员模态框
                $(".logo_select_div").click(function () {
                    $("#select_logo_modal").modal('show')
                })
                // excel导入客户
                $("#excel_input").on('change', function (e) {
                    var fileObj = e.currentTarget.files[0]; // js 获取文件对象
                    if (typeof (fileObj) == "undefined" || fileObj.size <= 0) {
                        alert("请选择文件");
                        return;
                    }
                    $(".input_file_name").show()
                    $(".input_file_name").text(fileObj.name)
                    $(".input_file_name").attr('title', fileObj.name)
                    let formData = new FormData();
                    formData.append("fileType_file", fileObj);
                    var index = layer.load(1, {
                        shade: [0.1, '#fff'] //0.1透明度的白色背景
                    });
                    $.ajax({
                        url: Domain + "/AddExcelByShop?filetype=DCMSCustomer",
                        type: "POST",
                        data: formData,
                        cache: false,
                        processData: false,
                        contentType: false,
                        beforeSend: function (request) {
                            request.setRequestHeader("Authorization", Authorization);
                        },
                        success: function (res) {
                            if (res) {
                                if (res.Response.Code == 200) {
                                    var data = res.Response.Data;
                                    $.ajax({
                                        url: Domain + "/ImportSubmitData",
                                        type: "post",
                                        beforeSend: function (request) {
                                            request.setRequestHeader("Authorization", Authorization);
                                        },
                                        data: {
                                            Type: 'DCMSCustomer', File: data.Path
                                        },
                                        success: function (result) {
                                            if (result) {
                                                if (result.Response.Code == 200) {
                                                    layer.close(index)
                                                    $("#excel_input").val("");
                                                    ajaxPost();
                                                    ajaxpostMember();
                                                    layer.msg('导入成功')
                                                }
                                                else {
                                                    $("#excel_input").val("");
                                                    layer.close(index)
                                                    ajaxPost();
                                                    ajaxpostMember();
                                                    FindLoginState(result.Response.Msg, "ImportSubmitData")
                                                }
                                            }
                                        },
                                        error: function (e) {
                                            console.log(e.status);
                                            console.log(e.responseText);
                                        }
                                    });
                                }
                                else {
                                    layer.msg("上传失败," + res.Response.Msg);
                                }
                            }
                        },
                        error: function (e) {
                            console.log(e.status);
                            console.log(e.responseText);
                        }
                    });
                });
            })
        })
        var getusernamelist = [];
        for (var i = 0; i < GetUserName.length; i++) {
            getusernamelist.push(GetUserName[i].id)
        }
        let Page = {
            "Page_id": Page_id,
            "Page_size": Page_size
        }
        let postdata = {
            "ModelName": ModelName,
            "GetUserName": getusernamelist,
            "StartTime": StartTime,
            "EndTime": EndTime,
            "Page": Page
        }
        //导出 DCMS全部客户
        $("#exporting_table").click(function () {
            var url = Domain + '/EXPORTDCMSCustomerList';
            downloadModel(postdata, url, "POST", "企业全部客户");
        });
        // 模板
        $(".download_template").click(function () {
            var url = Domain + '/EXPORTTemplate?FileName=102';
            getModelGet(url, "DCMS客户模板");
        });
    </script>
</body>

</html>